package view;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Vector;


import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.plaf.metal.MetalBorders.TableHeaderBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import controller.Sistema;
import model.Consorcio;
import model.Departamento;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JGenerarDetalleDeuda extends javax.swing.JFrame {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JPanel jPanelResumen;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JTable display;
	private JScrollPane jScrollPane1;
	private JLabel jLabel8;
	private JLabel jLabel7;
	private JButton btnCalcular;
	private JLabel jLabel6;
	private JButton btnCerrar;
	private JButton btnImprimir;
	private JPanel jPanelDetalle;
	private JComboBox jComboUF;
	private JComboBox jComboConsorcios;
	private static JGenerarDetalleDeuda instancia;

	//combos
	private Vector<Consorcio> consorcios;
	private Vector<Departamento> departamentos;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JGenerarDetalleDeuda inst = new JGenerarDetalleDeuda();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public JGenerarDetalleDeuda() {
		super();
		initGUI();
	}
	private void limpiarPantalla(){
		jLabel5.setText("");
		jLabel6.setText("");
		jLabel8.setText("");
		
		String[] colNames = {"Periodo","Concepto","Subtotal"};
		TableModel jTable1Model = new DefaultTableModel(null,colNames);
		display.setModel(jTable1Model);
	}
	
	public static JGenerarDetalleDeuda getInstancia(){
		if (instancia == null)
			instancia = new JGenerarDetalleDeuda();
		return instancia;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Detalle de deuda");
			getContentPane().setLayout(null);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Consorcio");
				jLabel1.setBounds(12, 17, 73, 16);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Unidad Funcional");
				jLabel2.setBounds(12, 49, 106, 16);
			}
			{
				
				consorcios = Sistema.getInstancia().getConsorcios();
				//Armar texto							
				Vector<String> nombres = new Vector<String>();
				if(consorcios!=null){
				for (int i=0;i<consorcios.size();i++){
					nombres.add(consorcios.elementAt(i).getNombre());
				}
				}
				
				ComboBoxModel jComboConsorciosModel = 	new DefaultComboBoxModel(nombres);
				jComboConsorcios = new JComboBox();
				getContentPane().add(jComboConsorcios);
				jComboConsorcios.setModel(jComboConsorciosModel);
				jComboConsorcios.setBounds(146, 14, 221, 23);
				jComboConsorcios.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
												
						departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());	
						Vector<String> unidades = new Vector<String>();
						if (departamentos != null){
							for (int i=0;i<departamentos.size();i++){
								unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
							}
							jComboUF.removeAllItems();
							for(int i=0; i<unidades.size();i++){
								jComboUF.addItem(unidades.elementAt(i));
								}
							
						}
						
						
					}
				});
			}
			{
				
				departamentos = Sistema.getInstancia().getDepartamentos(jComboConsorcios.getSelectedItem().toString());
				//Armar texto
				Vector<String> unidades = new Vector<String>();
				if (departamentos != null){
					for (int i=0;i<departamentos.size();i++){
						unidades.add(String.valueOf((departamentos.elementAt(i).getUnidadFuncional())));
					}
					
				}
								
				ComboBoxModel jComboUFModel = new DefaultComboBoxModel(unidades);
				jComboUF = new JComboBox();
				getContentPane().add(jComboUF);
				jComboUF.setModel(jComboUFModel);
				jComboUF.setBounds(146, 46, 221, 23);
			}
			
				
			
			{
				jPanelResumen = new JPanel();
				jPanelResumen.setLayout(null);
				getContentPane().add(jPanelResumen);
				jPanelResumen.setBounds(12, 116, 414, 68);
				jPanelResumen.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				{
					jLabel3 = new JLabel();
					jPanelResumen.add(jLabel3);
					jLabel3.setText("Propietario:");
					jLabel3.setBounds(13, 13, 76, 16);
				}
				{
					jLabel4 = new JLabel();
					jPanelResumen.add(jLabel4);
					jPanelResumen.add(getJLabel5());
					jPanelResumen.add(getJLabel6());
					jPanelResumen.add(getJLabel7());
					jPanelResumen.add(getJLabel8());
					jLabel4.setText("Departamento:");
					jLabel4.setBounds(243, 13, 91, 16);
				}
			}
			{
				jPanelDetalle = new JPanel();
				getContentPane().add(jPanelDetalle);
				jPanelDetalle.setBounds(12, 196, 414, 102);
				jPanelDetalle.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				jPanelDetalle.setLayout(null);
				jPanelDetalle.add(getJScrollPane1());
			}
			{
				btnImprimir = new JButton();
				getContentPane().add(btnImprimir);
				btnImprimir.setText("Imprimir");
				btnImprimir.setBounds(118, 310, 90, 23);
			}			
			{
				btnCerrar = new JButton();
				getContentPane().add(btnCerrar);
				getContentPane().add(getBtnCalcular());
				btnCerrar.setText("Cerrar");
				btnCerrar.setBounds(219, 310, 93, 23);
				btnCerrar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			pack();
			this.setSize(454, 382);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getJLabel5() {
		if(jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(85, 13, 151, 16);
		}
		return jLabel5;
	}
	
	private JLabel getJLabel6() {
		if(jLabel6 == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(337, 13, 64, 16);
		}
		return jLabel6;
	}
	
	private JButton getBtnCalcular() {
		if(btnCalcular == null) {
			btnCalcular = new JButton();
			btnCalcular.setText("Calcular");
			btnCalcular.setBounds(146, 81, 89, 23);
			btnCalcular.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					
					Calendar fecha = Calendar.getInstance();
					BigDecimal interes = new BigDecimal(0.05);
					int numero = 0;
					numero = Sistema.getInstancia().generarDetalleDeuda(fecha,interes.setScale(2, 1),Integer.valueOf((jComboUF.getSelectedItem().toString())), jComboConsorcios.getSelectedItem().toString());
								
					if (numero==0){
						Component frame = null;
						JOptionPane.showMessageDialog(frame, "El propietario no posee deudas");
						limpiarPantalla();
					}else{
					
					jLabel5.setText(Sistema.getInstancia().getPropietario(jComboConsorcios.getSelectedItem().toString(), Integer.valueOf(jComboUF.getSelectedItem().toString())));	
					jLabel6.setText(Sistema.getInstancia().getDepartamento(jComboConsorcios.getSelectedItem().toString(), Integer.valueOf(jComboUF.getSelectedItem().toString())));
					jLabel8.setText(Sistema.getInstancia().getTotalDetalleDeuda(jComboConsorcios.getSelectedItem().toString(), numero));
					
					//String[] colNames = {"Periodo","Concepto","Monto","Interes"};
					
					Vector<String> columNames = new Vector<String>(4);
					columNames.addElement("Periodo");
					columNames.addElement("Concepto");
					columNames.addElement("Subtotal");
						
					DefaultTableModel model = new DefaultTableModel(Sistema.getInstancia().obtenerDatosDeuda(jComboConsorcios.getSelectedItem().toString(), numero), columNames);
					//DefaultTableModel model = new DefaultTableModel(Sistema.getInstancia().obtenerDatosData(jComboConsorcios.getSelectedItem().toString(), numero), colNames);
					display.setModel(model);
					
					}//fin else
					
				}
				
			});
		}
		return btnCalcular;
	}

	private JLabel getJLabel7() {
		if(jLabel7 == null) {
			jLabel7 = new JLabel();
			jLabel7.setText("Total:");
			jLabel7.setBounds(13, 41, 41, 16);
		}
		return jLabel7;
	}
	
	private JLabel getJLabel8() {
		if(jLabel8 == null) {
			jLabel8 = new JLabel();
			jLabel8.setBounds(53, 41, 109, 16);
		}
		return jLabel8;
	}
	
	private JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(22, 7, 372, 87);
			jScrollPane1.setViewportView(getJTable1());
		}
		return jScrollPane1;
	}
	
	private JTable getJTable1() {
		if(display == null) {
			
			String[] colNames = {"Periodo","Concepto","Subtotal"};
			
			TableModel jTable1Model = new DefaultTableModel(null,colNames);
			display = new JTable();
			display.setModel(jTable1Model);
			getJTable1().getTableHeader().setAutoscrolls(true);
		}
		return display;
	}
	


}
